import threading
from threading import Lock, Thread
import time
import os


class TestThread(threading.Thread):
    def __init__(self, content):
        super(TestThread, self).__init__()
        self.content = content

    def run(self):
        print('task', self.content)
        time.sleep(1)
        print('2s')
        time.sleep(1)
        print('1s')
        time.sleep(1)
        print('0s')
        time.sleep(1)


def run(content):
    print('task', content)
    time.sleep(1)
    print('2s')
    time.sleep(1)
    print('1s')
    time.sleep(1)
    print('0s')
    time.sleep(1)


def example(major):
    add_sum = {major: []}
    print('专业：' + major)
    time.sleep(1)
    # for i in range(10000000):
    #     add_sum[name].append(i)
    print("add_sum done!")
    print("摸鱼5秒钟，工作1秒钟 at %s" % time.ctime(), end='\n')


if __name__ == '__main__':
    # thread_01 = TestThread("t1")
    # thread_02 = TestThread("t2")
    # start_01 = time.time()
    # thread_01.start()
    # thread_02.start()
    # thread_01.join()
    # thread_02.join()
    # end_01 = time.time()
    # print(end_01 - start_01)
    #
    # start_02 = time.time()
    # run("t1")
    # run("t2")
    # end_02 = time.time()
    # print(end_02-start_02)
####################################################################################
    names = [
        "Meteorology",
        "Climatology",
        "Atmospheric physics",
        "Atmospheric environment",
        "Atmospheric remote sensing",
    ]
    start = time.time()
    for name in names:
        example(name)

    end = time.time()
    print('运行时间：', end - start, '秒')

    # 创建线程数组
    threads = []
    for name in names:
        # 此处example为之前定义的函数，kwargs用于设定输入example函数的参数
        # 以字典形式输入可以同时设置多个参数
        t = threading.Thread(target=example, kwargs={"major": name})
        threads.append(t)

    start = time.time()
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    end = time.time()
    print('运行时间：', end - start, '秒')
